<?php
class BannerOffers extends Model_Abstract
{
	public function getBannerOffers($banner = null, $siteId = null, $today = null, $isDefault = null)
	{
		$vars = array();
		$query = array();
		if(!empty($banner)){
			$query[] = '`banner`=? ';
			$vars[] = $banner;
		}
		if($siteId !== null){
			$query[] = '(`site_id`=? OR `site_id`=0) ';
			$vars[] = $siteId;
		}
		if(!empty($today)){
			$query[] = '`start_date`<=? ';
			$vars[] = $today;
			
			$query[] = '`end_date`>=? ';
			$vars[] = $today;
		}
		if($isDefault !== null){
			$query[] = '`is_default`=? ';
			$vars[] = $isDefault;
		}
		if(!empty($query)){
			$sqlWhere = ' WHERE deleted=0 AND actived=1 AND ' . implode(' AND ', $query);
		}
		
		try {
			$sql = " SELECT * FROM banner_offers " . $sqlWhere;
			return $this->_connection->query($sql, $vars);
		} catch (Exception $e) {
			Qlog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), Qlog::ERR);
			throw new Model_Exception($e->getMessage, $e->getCode());
		}		
	}
	
	public function addBannerOffer($banner, $siteId, $offerName, $partnerName, $pictureUrl, $linkUrl, $startDate, $endDate) {
		$sql = 'INSERT INTO banner_offers SET banner=?, site_id=?, offer_name=?, partner_name=?, picture_url=?, link_url=?, start_date=?, end_date=?, update_time=?';
		try {
			return $this->_connection->execute($sql, array($banner, $siteId, $offerName, $partnerName, $pictureUrl, $linkUrl, $startDate, $endDate, time()));
		} catch (Exception $e) {
			Qlog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), Qlog::ERR);
			throw new Model_Exception($e->getMessage, $e->getCode());
		}
	}
	
	public function getBannerOfferById ($id) {
		$sql = 'SELECT * FROM banner_offers WHERE id=?';
		try {
			$reuslt = $this->_connection->query($sql, array($id));
			if (count($reuslt) > 0) {
				return $reuslt[0];
			}
			return null;
		} catch (Exception $e) {
			Qlog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), Qlog::ERR);
			throw new Model_Exception($e->getMessage, $e->getCode());
		}
	}
	
	public function updateBannerOffer($banner, $siteId, $offerName, $partnerName, $pictureUrl, $linkUrl, $startDate, $endDate, $id) {
		$sql = 'UPDATE banner_offers SET banner=?, site_id=?, offer_name=?, partner_name=?, picture_url=?, link_url=?, start_date=?, end_date=?, update_time=? WHERE id=?';
		try {
			return $this->_connection->execute($sql, array($banner, $siteId, $offerName, $partnerName, $pictureUrl, $linkUrl, $startDate, $endDate, time(), $id));
		} catch (Exception $e) {
			Qlog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), Qlog::ERR);
			throw new Model_Exception($e->getMessage, $e->getCode());
		}
	}
	
	public function deleteBannerOffer($id) {
		$sql = ' UPDATE banner_offers SET deleted=1 WHERE id=? ';
		try {
			return $this->_connection->execute($sql, array($id));
		} catch (Exception $e) {
			Qlog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), Qlog::ERR);
			throw new Model_Exception($e->getMessage, $e->getCode());
		}
	}
	
	public function incImpression($id) {
		$sql = ' UPDATE banner_offers SET impression=impression+1 WHERE id=? ';
		try {dump($sql);
			return $this->_connection->execute($sql, array($id));
		} catch (Exception $e) {
			Qlog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), Qlog::ERR);
			throw new Model_Exception($e->getMessage, $e->getCode());
		}
	}
}